define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'main/area/index?pid='+Config.pid + location.search,
                    add_url: 'main/area/add?pid='+Config.pid,
                    edit_url: 'main/area/edit',
                    del_url: 'main/area/del',
                    multi_url: 'main/area/multi',
                    import_url: 'main/area/import',
                    table: 'area',
                }
            });

            var table = $("#table");

			var lv1=Config.level*1+1;
			console.log(Config.level)
			var btns=[{
				name: 'message',
				text: __(''),
				title: __('地区'), 
				classname: 'btn btn-xs btn-default btn-dialog',
				icon: 'fa fa-list',
				url: 'main/area/index?pid={id}&level='+lv1,
				extend:'data-area=\'["80%","80%"]\'',
				callback: function (data) {
				},
				visible: function (row) {
					if(Config.level<2)
						return true;
					else 
						return false;
				} 
			}];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
				search:false,
				showColumns: false,
				showToggle: false,
				// commonSearch: false,
				showExport: false,
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        // {field: 'pid', title: __('Pid')},
                        {field: 'shortname', title: __('Shortname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mergename', title: __('Mergename'), operate: 'LIKE'},
                        // {field: 'level', title: __('Level'), searchList: {"1":__('Level 1'),"2":__('Level 2'),"3":__('Level 3')}, formatter: Table.api.formatter.normal},
                        {field: 'pinyin', title: __('Pinyin'), operate: 'LIKE'},
                        // {field: 'code', title: __('Code'), operate: 'LIKE'},
                        {field: 'zip', title: __('Zip'), operate: 'LIKE'},
                        // {field: 'first', title: __('First'), operate: 'LIKE'},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:btns}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
